from django.shortcuts import render
from rest_framework import permissions, decorators

from utils.customer_modelviewset import CustomerViewSet
from utils import response_ok, response_error
from .models import Menu, PermissionGroup, Permission, Role
from .serializers import (MenuSerializer, PermissionGroupSerializer, PermissionSerializer, RoleSerializer,
                          MenuDictSerializer, PermissionGroupDictSerializer)


# Create your views here.
# 菜单
class MenuViewSet(CustomerViewSet):
    permission_classes = [permissions.IsAdminUser, ]
    queryset = Menu.objects.filter()
    serializer_class = MenuSerializer

    @decorators.action(methods=['get'], detail=False)
    def get_dict(self, request):
        return response_ok(MenuDictSerializer(instance=self.queryset, many=True).data)

    # 权限组


class PermissionGroupViewSet(CustomerViewSet):
    permission_classes = [permissions.IsAdminUser, ]
    queryset = PermissionGroup.objects.filter()
    serializer_class = PermissionGroupSerializer

    @decorators.action(methods=['get'], detail=False)
    def get_dict(self, request):
        ret = {
            'methods': Permission.METHOD_CHOICE_DICT,
            'permission_group': PermissionGroupDictSerializer(instance=self.queryset, many=True).data
        }
        return response_ok(ret)


# 权限
class PermossionViewSet(CustomerViewSet):
    permission_classes = [permissions.IsAdminUser, ]
    queryset = Permission.objects.filter()
    serializer_class = PermissionSerializer


# 角色
class RoleViewSet(CustomerViewSet):
    permission_classes = [permissions.IsAdminUser, ]
    queryset = Role.objects.filter()
    serializer_class = RoleSerializer
